package com.shangguan.spring.realm;

import com.shangguan.spring.beans.User;
import com.shangguan.spring.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName: UserRealm
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 05 月 19 14:45
 * @Version 1.0
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;
    public UserRealm() {
        super();
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
            throws AuthenticationException {
        String username = (String)token.getPrincipal();
        //根据用用户名查信息
           User user = userService.selectByUserName(username);
           if(user==null){
               return  null;
           }
        ByteSource byteSource = ByteSource.Util.bytes(username);
        SimpleAuthenticationInfo simpleAuthenticationInfo =
                new SimpleAuthenticationInfo(username,user.getUserpassword(),byteSource,this.getName());
        return simpleAuthenticationInfo;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }
}
